package com.hpeng.daily.action;

import java.io.UnsupportedEncodingException;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.apache.struts2.ServletActionContext;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;

import com.hpeng.daily.service.AssetCardManager;
import com.hpeng.sys.model.AssetCard;
import com.hpeng.sys.model.Category;
import com.hpeng.sys.service.Function;
import com.hpeng.sys.service.LoginUserMgr;
import com.hpeng.sys.vo.Lookup;
import com.hpeng.util.Pagination;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;

@Component("assetCardCheckAction")
@Scope("prototype")
public class AssetCardCheckAction extends ActionSupport {
	private static final long serialVersionUID = 1L;
	// 用于分页显示
	private int page=1;
	public String pageLink;
	private Pagination<AssetCard> pagination;
	
	private List<Lookup> ls;
	private String msg;
	
	//用于根据权限生成功能按钮
	@Resource
	private Function function;
	@Resource
	private LoginUserMgr loginUser;
	private String accessRight;	
	private String FN;
	private String userDepName;


	//资产卡片的业务层
	@Resource
	private AssetCardManager assetCardManager;
	
	private AssetCard assetCard;	//资产卡片
	private String acid;	//物品编号
	private String catid;
	
	public Pagination<AssetCard> getPagination() {
		return pagination;
	}
	public void setPagination(Pagination<AssetCard> pagination) {
		this.pagination = pagination;
	}
	public void setPage(int page) {
		this.page = page;
	}
	public int getPage(){
		return page;
	}
	public void setAssetCardManager(AssetCardManager assetCardManager) {
		this.assetCardManager = assetCardManager;
	}
	public void setAssetCard(AssetCard assetCard) {
		this.assetCard = assetCard;
	}
	public void setAcid(String acid) {
		this.acid = acid;
	}
	public String getAcid() {
		return acid;
	}
	public String getMsg() {
		return msg;
	}

	public void setMsg(String msg) {
		this.msg = msg;
	}
	public void setFN(String fN) {
		FN = fN;
	}
	public String getFN() {
		return FN;
	}
	public String getUserDepName() {
		return userDepName;
	}
	public void setUserDepName(String userDepName) {
		this.userDepName = userDepName;
	}
	public Map<String,Object> session(){
		return ActionContext.getContext().getSession();
	}
	public HttpServletRequest request(){
		return ServletActionContext.getRequest();
	}
	public String getAccessRight() {
		return accessRight;
	}
	public void setAccessRight(String accessRight) {
		this.accessRight = accessRight;
	}
	public List<Lookup> getLs() {
		return ls;
	}
	public void setLs(List<Lookup> ls) {
		this.ls = ls;
	}
	public String getCatid() {
		return catid;
	}
	public void setCatid(String catid) {
		this.catid = catid;
	}
	
	//查询登录员所在部门处于在用和进入盘点的资产
	public String searchCheckAsset(){
		HttpServletRequest request = ServletActionContext.getRequest();
		this.userDepName=loginUser.getUserDeptName(request);
		//只能查询出登录员所在的部门处于在用和进入盘点的资产
		String[] hql = {" from AssetCard where acstatus='400' and depname='"+userDepName+"' or acstatus like '9%' and depname='"+userDepName+"' or acstatus like '9%' and reportdepartment='"+userDepName+"'"};
		String order = " order by acid asc ";
		setPagination(assetCardManager.getPagination(page, hql, order));
		pageLink = pagination.getPageLink("searchCheckAsset.action?FN=" + FN);
		
		this.accessRight = function.getAccessRight(session(), FN);
		return "success";
	}
	
	// 盘盈资产信息录入
	public String readInInfoSurplus() {
		try {
			//资产类别
			Category c = new Category();
			c.setCatid(catid);
			assetCard.setCategory(c);
			assetCard.setAcstatus("901");
			assetCardManager.addAssetCard(assetCard, "A", loginUser.getUserId(request()),function.getStatusDesc("9", "01"));
			this.accessRight = function.getAccessRight(session(), FN);
		} catch (RuntimeException e) {
			msg = getText("录入失败，请重新操作");
		}
		if (msg == null || "".equals(msg.trim())) {
			msg = getText("录入成功");
		}
		return searchCheckAsset();
	}
	
	public String getPageLink() {
		return pageLink;
	}
	public void setPageLink(String pageLink) {
		this.pageLink = pageLink;
	}
	public AssetCard getAssetCard() {
		return assetCard;
	}
	// 盘盈入库申请
	public String inputWarehouseSurplus() {
		try{
			HttpServletRequest request = ServletActionContext.getRequest();
			String acidAll=request.getParameter("acidAll");
			if(!"".equals(acidAll)){
				String[] dids =acidAll.split(",");
				for(String id : dids){
					assetCard=assetCardManager.findAssetCard(id);
					//入库申请的只能是本部门状态处于盘盈资产信息已录入的资产
					if("901".equals(assetCard.getAcstatus())){
						assetCard.setAcstatus("902");
						assetCard.setDepname(assetCard.getReportdepartment());
						//写入台帐记录
						assetCardManager.updateAssetCard(assetCard,"E",loginUser.getUserId(request()),null,function.getStatusDesc("9", "02"));
					}else{
						msg=getText("入库申请的只能是本部门状态处于盘盈资产信息已录入的资产");
					}
				}
			}
		} catch (Exception rx) {
			rx.printStackTrace();
			msg = getText("申请失败，请重新操作");
		}
		if (msg == null || "".equals(msg.trim())) {
			msg = getText("申请成功");
		}
		
		return searchCheckAsset();
	}
	
	//盘亏申报（盘亏申报的只能是登录的用户所在的部门下的处于在用的资产）
	public String declareLost(){
		try{
			HttpServletRequest request = ServletActionContext.getRequest();
			String acidAll=request.getParameter("acidAll");
			if(!"".equals(acidAll)){
				String[] dids =acidAll.split(",");
				for(String id : dids){
					assetCard=assetCardManager.findAssetCard(id);
					//盘亏申报的只能是登录的用户所在的部门下的处于在用的资产
					if("400".equals(assetCard.getAcstatus())){
						assetCard.setAcstatus("903");
						//写入台帐记录
						assetCardManager.updateAssetCard(assetCard,"E",loginUser.getUserId(request()),null,function.getStatusDesc("9", "03"));
					}else{
						msg=getText("经办申报的只能是本部门状态处于在用的资产");
					}
				}
			}
		} catch (Exception rx) {
			msg = getText("申报失败，请重新操作");
		}
		if (msg == null || "".equals(msg.trim())) {
			msg = getText("申报成功");
		}
		return searchCheckAsset();
	}
	
	//盘亏财务部审核（由财务部的经办审核）
	public String declarationLost(){
		try{
			HttpServletRequest request = ServletActionContext.getRequest();
			String acid=request.getParameter("acid");
			String ideaValue=new String(request.getParameter("ideaValue").getBytes("iso8859-1"),"UTF-8");
			if(!"".equals(acid)){
				assetCard=assetCardManager.findAssetCard(acid);
				//判断只能审核经办盘亏已申报的资产(审核只能单个完成，因为需要建议)
				if("903".equals(assetCard.getAcstatus())){
					assetCard.setAcstatus("904");
					assetCardManager.updateAssetCard(assetCard,"E",loginUser.getUserId(request()),ideaValue,function.getStatusDesc("9", "04"));
				}else{
					msg=getText("只能审核经办已申报的资产");
				}
			}
		} catch (Exception rx) {
			msg = getText("审核失败，请重新操作");
		}
		if (msg == null || "".equals(msg.trim())) {
			msg = getText("审核成功");
		}
		
		return searchCheckAsset();
	}
	
	//盘亏财务部退回（由财务部的经办退回）
	public String declarationUntreadLost(){
		try{
			HttpServletRequest request = ServletActionContext.getRequest();
			String acid=request.getParameter("acid");
			String ideaValue=new String(request.getParameter("ideaValue").getBytes("iso8859-1"),"UTF-8");
			if(!"".equals(acid)){
				assetCard=assetCardManager.findAssetCard(acid);
				//判断只能退回经办盘亏已申报的资产(退回只能单个完成，因为需要建议)
				if("903".equals(assetCard.getAcstatus())){
					assetCard.setAcstatus("400");
					assetCardManager.updateAssetCard(assetCard,"E",loginUser.getUserId(request()),ideaValue,function.getStatusDesc("4", "00"));
				}else{
					msg=getText("只能退回经办已申报的资产");
				}
			}
		} catch (Exception rx) {
			msg = getText("退回失败，请重新操作");
		}
		if (msg == null || "".equals(msg.trim())) {
			msg = getText("退回成功");
		}
		
		return searchCheckAsset();
	}
	
	//盘亏财务部上报（由财务部领导上报）
	public String reportLost(){
		try{
			HttpServletRequest request = ServletActionContext.getRequest();
			String acidAll=request.getParameter("acidAll");
			if(!"".equals(acidAll)){
				String[] dids =acidAll.split(",");
				for(String id : dids){
					assetCard=assetCardManager.findAssetCard(id);
					//盘亏财务部上报的只能是处于盘亏财务部已审核的资产
					if("904".equals(assetCard.getAcstatus())){
						assetCard.setAcstatus("905");
						//写入台帐记录
						assetCardManager.updateAssetCard(assetCard,"E",loginUser.getUserId(request()),null,function.getStatusDesc("9", "05"));
					}else{
						msg=getText("经办上报的只能是本部门状态处于盘亏财务部已审核的资产");
					}
				}
			}
		} catch (Exception rx) {
			msg = getText("上报失败，请重新操作");
		}
		if (msg == null || "".equals(msg.trim())) {
			msg = getText("上报成功");
		}
		
		return searchCheckAsset();
	}
	
	//盘亏确认（由本部门的领导确认）
	public String validateLost(){
		try{
			HttpServletRequest request = ServletActionContext.getRequest();
			String acid=request.getParameter("acid");
			String ideaValue=new String(request.getParameter("ideaValue").getBytes("iso8859-1"),"UTF-8");
			if(!"".equals(acid)){
				assetCard=assetCardManager.findAssetCard(acid);
				//判断只能确认财务部领导已上报的资产(确认只能单个完成，因为需要建议)
				if("905".equals(assetCard.getAcstatus())){
					assetCard.setAcstatus("906");
					assetCardManager.updateAssetCard(assetCard,"E",loginUser.getUserId(request()),ideaValue,function.getStatusDesc("9", "06"));
				}else{
					msg=getText("只能确认财务部领导已上报的资产");
				}
			}
		} catch (Exception rx) {
			msg = getText("确认失败，请重新操作");
		}
		if (msg == null || "".equals(msg.trim())) {
			msg = getText("确认成功");
		}
		
		return searchCheckAsset();
	}
	
	//盘亏退回（由本部门的领导退回）
	public String validateUntreadLost(){
		try{
			HttpServletRequest request = ServletActionContext.getRequest();
			String acid=request.getParameter("acid");
			String ideaValue=new String(request.getParameter("ideaValue").getBytes("iso8859-1"),"UTF-8");
			if(!"".equals(acid)){
				assetCard=assetCardManager.findAssetCard(acid);
				//判断只能退回财务部领导已上报的资产(退回只能单个完成，因为需要建议)
				if("905".equals(assetCard.getAcstatus())){
					assetCard.setAcstatus("904");
					assetCardManager.updateAssetCard(assetCard,"E",loginUser.getUserId(request()),ideaValue,function.getStatusDesc("9", "04"));
				}else{
					msg=getText("只能退回财务部领导已上报的资产");
				}
			}
		} catch (Exception rx) {
			msg = getText("退回失败，请重新操作");
		}
		if (msg == null || "".equals(msg.trim())) {
			msg = getText("退回成功");
		}
		
		return searchCheckAsset();
	}

	//条件查询
	public String getCheckAssetCardByPageNumberAndValue(){
		HttpServletRequest request = ServletActionContext.getRequest();
		this.userDepName=loginUser.getUserDeptName(request);
		String propertyValue=request.getParameter("propertyValue");
		String pageNumberStr=request.getParameter("page");
		if(pageNumberStr!=null){
			page=Integer.parseInt(pageNumberStr);
		}
		String[] hql=new String[1];
		if("acstatus".equals(propertyValue)){
			try{
				String propertyValueBeChoose=request.getParameter("propertyValueBeChoose");
				hql[0] = " from AssetCard where acstatus='400' and "+propertyValue+" like '%"+propertyValueBeChoose+"%' and depname='"+userDepName+"' or acstatus like '9%' and "+propertyValue+" like '%"+propertyValueBeChoose+"%' and depname='"+userDepName+"'";
				String order = " order by acid asc ";
				setPagination(assetCardManager.getPagination(page, hql, order));
				pageLink = pagination.getPageLink("getInnerMoveAssetCardByPageNumberAndValue.action?FN=" + FN+"&propertyValue="+propertyValue+"&propertyValueBeChoose="+propertyValueBeChoose);
			} catch (Exception rx) {
				msg = getText("条件查询失败，请重新操作");
			}
		}else{
			String inputValue;
			try {
				inputValue = new String(request.getParameter("inputValue").getBytes("iso8859-1"),"UTF-8");
				StringBuffer strBuffer=new StringBuffer();
				strBuffer.append("%");
				for(int i=0;i<inputValue.length();i++){
					strBuffer.append(inputValue.charAt(i)+"%");
				}
				hql[0] = " from AssetCard where acstatus='400' and "+propertyValue+" like '"+strBuffer+"' and depname='"+userDepName+"' or acstatus like '9%' and "+propertyValue+" like '"+strBuffer+"' and depname='"+userDepName+"'";
				String order = " order by acid asc ";
				setPagination(assetCardManager.getPagination(page, hql, order));
				pageLink = pagination.getPageLink("getInnerMoveAssetCardByPageNumberAndValue.action?FN=" + FN+"&propertyValue="+propertyValue+"&inputValue="+inputValue);
			} catch (Exception rx) {
				msg = getText("条件查询失败，请重新操作");
			}
		}
		this.accessRight = function.getAccessRight(session(), FN);
		
		return "success";
	}
	
	//点击类别树上的类别得到本部门下该类别的处于盘点状态的资产
	public String queryAssetInCategoryTree() {
		HttpServletRequest request = ServletActionContext.getRequest();
		this.userDepName=loginUser.getUserDeptName(request);
		String catid=request.getParameter("catid");
		int page=1;
		if(request.getParameter("page")!=null){
			page=Integer.parseInt(request.getParameter("page"));
		}
		String[] hql = { " from AssetCard where acstatus='400' and catid like '"+catid+"%' and depname='"+userDepName+"' or acstatus like '9%' and catid like '"+catid+"%' and depname='"+userDepName+"'" };
		String order = " ";
		setPagination(assetCardManager.getPagination(page, hql, order));
		pageLink = pagination.getPageLink("queryAssetInnerMoveInTree.action?FN=" + FN+"&catid="+catid);
		
		this.accessRight = function.getAccessRight(session(), FN);
		return "success";
	}
	
	//点击部门树上的部门得到该部门下的处于盘点状态的资产（不能操作其他部门）
	public String queryAssetInDepartmentTree() {
		HttpServletRequest request = ServletActionContext.getRequest();
		this.userDepName=loginUser.getUserDeptName(request);
		String depname;
		try {
			depname = new String(request.getParameter("depname").getBytes("iso8859-1"),"UTF-8");
			if(userDepName.equals(depname)){
				int page=1;
				if(request.getParameter("page")!=null){
					page=Integer.parseInt(request.getParameter("page"));
				}
				String[] hql = { " from AssetCard where acstatus='400' and depname like '"+depname+"%' or acstatus like '9%' and depname like '"+depname+"%'" };
				String order = " ";
				setPagination(assetCardManager.getPagination(page, hql, order));
				pageLink = pagination.getPageLink("queryAssetInnerMoveInTree.action?FN=" + FN+"&depname="+depname);
			}else{
				msg = getText("查询操作失败，不能操作其他部门的资产");
			}
		} catch (UnsupportedEncodingException e) {}
		
		this.accessRight = function.getAccessRight(session(), FN);
		return "success";
	}
	
}
